package hp.com.servlet.ProductServlet;

import hp.com.entity.PageInfo;
import hp.com.entity.Product;
import hp.com.entity.Sells;
import hp.com.entity.Users;
import hp.com.service.ProductService;
import hp.com.service.impl.ProductServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

@WebServlet("/AllProducts")
public class SellServlet extends HttpServlet {
    private ProductService productService = new ProductServiceImpl();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        try {
            //通过访问的session来获取用户对象
            Users user=null;
            HttpSession session = req.getSession();
            if(session.getAttribute("userSession")!=null){
                user=(Users)session.getAttribute("userSession");
                req.setAttribute("userSession",user);}
            //如果没有页面属性，则默认前往第一页
            if (req.getParameter("pageNo")==null){
                PageInfo<Sells> page = new PageInfo<>(1,12);
                page = productService.listPage(page);
                req.setAttribute("page",page);
            }else {
                int pageNo = Integer.parseInt(req.getParameter("pageNo"));
                PageInfo<Sells> page = new PageInfo<>(pageNo,12);
                page = productService.listPage(page);
                req.setAttribute("page",page);
            }
            req.setAttribute("userSession",user);
            req.setAttribute("type","all");
            req.getRequestDispatcher(req.getContextPath()+"ShopPage/shop.jsp").forward(req,resp);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }
}
